package com.chuangshu.chuangshuteam.controller;

import com.chuangshu.chuangshuteam.annotation.RequiresPermissions;
import com.chuangshu.chuangshuteam.config.BaseContext;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.TeamInvitation;
import com.chuangshu.chuangshuteam.entity.TeamMember;
import com.chuangshu.chuangshuteam.service.TeamInvitationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/team/invitation")
public class TeamInvitationController {

    @Autowired
    private TeamInvitationService teamInvitationService;

    /**
     * 创建团队邀请
     */
    @PostMapping("/create")
    @RequiresPermissions("team:invite")
    public Result createInvitation(@RequestBody CreateInvitationRequest request) {
        Long currentUserId = BaseContext.getCurrentId();

        try {
            TeamInvitation invitation = teamInvitationService.createInvitation(
                    currentUserId,
                    request.getInviteeOpenid(),
                    request.getInviteeName(),
                    request.getDirection()
            );

            return Result.ok(invitation);
        } catch (SecurityException e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 接受团队邀请
     */
    @PostMapping("/accept")
    public Result acceptInvitation(@RequestParam String inviteCode) {
        Long currentUserId = BaseContext.getCurrentId();

        try {
            TeamMember teamMember = teamInvitationService.acceptInvitation(inviteCode, currentUserId);
            return Result.ok(teamMember);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 获取我的邀请列表
     */
    @GetMapping("/my")
    public Result getMyInvitations() {
        Long currentUserId = BaseContext.getCurrentId();

        List<TeamInvitation> invitations = teamInvitationService.getUserInvitations(currentUserId);
        return Result.ok(invitations);
    }

    /**
     * 撤销邀请
     */
    @PostMapping("/revoke/{invitationId}")
    public Result revokeInvitation(@PathVariable Long invitationId) {
        Long currentUserId = BaseContext.getCurrentId();

        try {
            boolean success = teamInvitationService.revokeInvitation(invitationId, currentUserId);
            if (success) {
                return Result.ok();
            } else {
                return Result.fail("撤销失败");
            }
        } catch (SecurityException e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 检查邀请码是否有效
     */
    @GetMapping("/check")
    public Result checkInvitation(@RequestParam String inviteCode) {
        boolean isValid = teamInvitationService.isInvitationValid(inviteCode);
        return Result.ok(isValid);
    }
}

/**
 * 创建邀请请求
 */
class CreateInvitationRequest {
    private String inviteeOpenid;
    private String inviteeName;
    private String direction;

    // getter and setter
    public String getInviteeOpenid() { return inviteeOpenid; }
    public void setInviteeOpenid(String inviteeOpenid) { this.inviteeOpenid = inviteeOpenid; }
    public String getInviteeName() { return inviteeName; }
    public void setInviteeName(String inviteeName) { this.inviteeName = inviteeName; }
    public String getDirection() { return direction; }
    public void setDirection(String direction) { this.direction = direction; }
}